How to set up working directory in PyCharm and package import?

已回答

I want 2 questions:

1) I want to import the file lr_utils.py as a package, it's in my current directory. PyCharm refuse to read it. So, I specified the absolute path,  It still doesn't work. Why?

os.chdir("/Users/Branden/Desktop/Deep Learning")

from lr_utils import load_dataset

If I ran the same code in another editor like Sublime Text, Spyder, or iPython, it works. Why PyCharm does not recognize the module?

 

2) I have packages such as numpy, matplotlib installed using anaconda, but why PyCharm does not recgonize it, the text is in grey color, I need to right-click on the package and install it again in PyCharm. Is it linking it or actually installing a dupulicate inside another folder?

2
Avatar
Permanently deleted user

I am aware of this post, which says you must configure the environment first.

But what if:

1) you are importing 10 packages from 10 different folders, do you have to configure 10 times? How about using os.dir() to specifiy the absolute path? It works in all other editors, why not PyCharm?

2) do you have to configure your environment every time you start a project? Also, what is the idea behind project? I just want to run a Python file, that's it. I don't want the trouble of building a project!

 

 

2
Avatar
Permanently deleted user

Anyone? Any ideas?

0

Hi brandenju! I believe os.chdir doesn't affect PYTHONPATH so changing your working directory at runtime to resolve imports make little sense. One can edit PYTHONPATH with sys.path object if needed.

PyCharm refuse to read it

Could you please specify what exactly happened? Do you receive an unresolved reference error, ImportError during the runtime or both?

it's in my current directory

Is this directory a project root? If it's not then the problem seems to be clear - when running a file in the terminal your current working directory is added to the PYTHONPATH, this is not the case in PyCharm - by default, only the project root is added to PYTHONPATH. The basic workaround here is to mark lr_utils.py folder as Sources Root by right click in the Project window.

All source roots are added to PYTHONPATH.

9
I have packages such as ..., but why PyCharm does not recgonize it, the text is in grey color

By default grey color indicates unused (not unresolved) imports. Could you please provide a screenshot?

Is it linking it or actually installing a dupulicate inside another folder?

PyCharm installs the package inside current project interpreter, which is selected in Settings | Project ... | Project Interpreter.

you are importing 10 packages from 10 different folders, do you have to configure 10 times?

Project interpreter is selected to indicate to PyCharm which Python executable should be used to run scripts and where to find packages installed from PyPI. Local package imports should be resolved according to project structure and PYTHONPATH. Could you please provide an example structure to illustrate the problem?

It works in all other editors, why not PyCharm?

Most other editors don't really handle interaction with Python interpreter, you are probably running your code in the terminal.

do you have to configure your environment every time you start a project?

For a new project - yes you do. PyCharm has to know which interpreter to use to run your scripts, to analyze your code for errors and so on.

I just want to run a Python file, that's it. I don't want the trouble of building a project!

You can run it in the terminal then, PyCharm provides code insight for edited files and it's impossible to do for an unknown interpreter. If I correctly understand your question.

0
Avatar
Permanently deleted user

Hi,

I am aware of adding a path to the project, but I can't tell somebody else with my code to do that, it's NOT what I am looking for.

Here's a screenshot of my folder,

 

I just want to import lr_utis.py. I am working on file Logistic_Regression.py. All of the files are in the SAME folder.

Last time at Kaggle, the judge was having problems changing path on his end. I can't tell him: "hey, right click on project window in PyCharm and add path to root".

The idea of creating a project in include your files is horrible. I have my Python code, just let me run them and get on with my day.

2

I assume both these files are not in the PyCharm project root and please correct me if this is not correct. So here is the problem: PyCharm doesn't know you are going execute Logistic_Regression.py in some directory foo in which case lr_utils.py imports are valid because foo is added to PYTHONPATH. So you "help" it to figure it out by marking the folder as Sources Root. It has nothing to do with project execution outside of PyCharm, so I'm not sure why Kaggle judge have to do anything with it.

Could you please provide more details about your project? I mean which script is an entry point? How does import structure look like? Comments on the forum have poor formatting, so please feel free to create a ticket in our bug tracker if it's more convenient for you.

I look forward to your reply.

0
Avatar
Permanently deleted user

Not trying to hijack this thread, but I maybe had a similar problem.  Certain packages installed in the site-packages directory on Win10 would not be included in the External Libraries within the project.  Tried running as administrator and no different.  Same app imported into a PyCharm project worked on Win7.  The missing packages were mx.DateTime, xlrd, and xlwt.  Python command line worked fine.

Issue ended up being the 2017.3 version of the code.  Went back to 2017.2.4 on WIn10 and it found the packages as it did on Win7.  Not sure if the problem occurs on Win7 with 2017.3.  App works fine now...

1
Avatar
Permanently deleted user

I just want to change the directory. On all other editor, this is what I get. Directory changed!

> import os
> os.chdir("/Users/Branden/Desktop/Deep Learning")
> os.getcwd()
'/Users/Braden/Desktop/Deep Learning'

Now, why does PyCharm refuse to do so? I find this troublesome in several ways:

  1. When someone else sends me his code, he changed directory using os package. For me, I have to search through the code to find out what are the paths, and set them to root.
  2. When I change to a different computer, I have to configure root path, again! That's not efficient!
  3. What's the point of change directory? When I say change path, I want change path. Nothing tricky.

Ultimately, I find this change root thing tedious, it's completely PyCharm depend. I remember a friend who was disqualified from Kaggle because they couldn't run his code. As it turned out, he was setting everything to root in PyCharm, but the judge wasn't using PyCharm.

1
Avatar
Permanently deleted user

Actually, I was able to change the directory, but that does nothing!

It does not actually lead Python to search in that path for a module I wrote.

So, I think PyCharm is actually loading files from an internal path?

0

I'm facing a similar issue and this is such a pain. I was able to import a custom library using the Python console in PyCharm. But if I run the same command 'import mylibrary' in a .py file, it does not run! I'm a relatively new Python user and I've had this same issue for weeks now and no proper answer. My work-around is to simply flatten out all folder structure and 'spill' all files in the project root folder (ugly).

Is there any step-by-step guide/documentation to set up and explain PyCharm projects?

0

Hi Milind, could you please provide an example of your project tree? Most likely you need to mark some folder as a Source Root to indicate it should be present in PYTHONPATH but it's hard to tell without details. Feel free to create a ticket in our bug tracker https://youtrack.jetbrains.com/issues/PY

-1

请先登录再写评论。