Python Forum
Importing Packages/Modules
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing Packages/Modules
#1
I know this is a basic question, and trust me, I have exhausted other resources and it still won't work.

I am unable to import any modules when running my python file when I execute my python file through Sublime Text, but I am able to when I interact with python.py via cmd prompt. I am not sure what has gone wrong. I have appended the sys.path to include my current directory and it still does not work. Below is my code:

import sys
sys.path.append('\Users\b\Box Sync\Desktop\Projects\Coding\pythonlessons')
print sys.path

import things

Below is my output:

C:\Users\b\Box Sync\Desktop\Projects\Coding\pythonlessons>testfile.py
['C:\\Users\\b\\Box Sync\\Desktop\\Projects\\Coding\\pythonlessons', 'C:\\Users\\b\\Box Sync\\Desktop\\Projects\\Coding\\pythonlessons', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', '\\Users\x08\\Box Sync\\Desktop\\Projects\\Coding\\pythonlessons']
Traceback (most recent call last):
File "C:\Users\b\Box Sync\Desktop\Projects\Coding\pythonlessons\testfile.py", line 6, in <module>
import things
ImportError: No module named things


The same thing happens for numpy or anything else. I would greatly appreciate your input.

Thank you,

In addition, since my python.py file works, I thought I could create a .py file in a different directory like C:\Python27 and see if I could access a module and the same reply comes up when I execute it.
Reply
#2
Just to make it clear you should not be using Python 2 Wink

Follow this Python 3.6 and pip installation under Windows
When this work pip install numpy work from anywhere in cmd.
Then import numpy works.

When this is done you can also access or install Python 2.7.
Python 3.6 install a py.exe in Windows folder.

Run and install to 2.7
# Run 
C:\1                                                                         
λ py -2.7 version.py                                                         
Hello from 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)]

# Install                                                                             
C:\1                             
λ py -2.7 -m pip install requests
Collecting requests
  Using cached requests-2.13.0-py2.py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.13.0
So now do pip and python always point to 3.6 from anywhere in cmd or cmder(as i use),
and py can access 2.7 or other version installed.
Example 3.4:
C:\1
λ py -3.4 version.py
Hello from 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)]
 
C:\1
λ py -3.4 -m pip install requests
Collecting requests
 Downloading requests-2.18.1-py2.py3-none-any.whl (88kB)
   100% |################################| 92kB 385kB/s
Successfully installed certifi-2017.4.17 chardet-3.0.4 idna-2.5 requests-2.18.1 urllib3-1.21.1 
Reply
#3
Should I uninstall Python 2.7 then?
Reply
#4
(Mar-27-2018, 02:10 AM)Upandon Wrote: Should I uninstall Python 2.7 then?
No you just follow link with installation of 3.6.
When that work you can also access 2.7 as shown trough use of py.
Reply
#5
Okay, I followed the instructions and it said it already installed - It is downloaded in my control panel. Do I reinstall everything or is there a way to make 3 default? Or will that even address the issue?
Reply
#6
You need to read the whole link,the last part test in cmd.
Restart pc.
Then follow step to see that python and pip point to 3.6 form anywhere in cmd.
Reply
#7
Thank you for your help. I have followed the link, and python 3 works, and when I import numpy from the >>> prompt, there is no error. Pip is installed. Yet, when I run my .py file nothing has changed. I still get:

C:\Users\b\Box Sync\Desktop\Projects\Coding\pythonlessons>testfile.py
Output:
['C:\\Users\\b\\Box Sync\\Desktop\\Projects\\Coding\\pythonlessons', 'C:\\Users\\b\\Box Sync\\Desktop\\Projects\\Coding\\pythonlessons', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages'] C:\Users\b\Box Sync\Desktop\Projects\Coding\pythonlessons>testfile.py Traceback (most recent call last): File "C:\Users\b\Box Sync\Desktop\Projects\Coding\pythonlessons\testfile.py", line 1, in <module> import numpy as np ImportError: No module named numpy C:\Users\b\Box Sync\Desktop\Projects\Coding\pythonlessons>

The first output is my sys.path still.

And when I check the sys.path in python 3, I get the following:

Output:
C:\Users\b\Box Sync\Desktop\Projects\Coding\pythonlessons λ python Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> print (sys.path) ['', 'C:\\Users\\b\\Box Sync\\Desktop\\Projects\\Coding\\pythonlessons', 'C:\\python\\python36.zip', 'C:\\python\\DLLs', 'C:\\python\\lib', 'C:\\python', 'C:\\python\\lib\\site-packages'] >>> exit() C:\Users\b\Box Sync\Desktop\Projects\Coding\pythonlessons λ testfile.py Traceback (most recent call last): File "C:\Users\b\Box Sync\Desktop\Projects\Coding\pythonlessons\testfile.py", line 1, in <module> import numpy as np ImportError: No module named numpy
Reply
#8
SOLVED:

I uninstalled both versions of python and reinstalled only Python 3 and now it imports just fine. I found the cmd script running one version and my .py files running another.

Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Importing Modules Have Stopped Working. Randomname 6 5,054 Jul-14-2018, 07:21 AM
Last Post: Randomname
  importing packages to pycharm robinhoodjr 2 6,408 Mar-07-2018, 08:37 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020